package com.anysoft.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.TextView;

import com.anysoft.R;
import com.anysoft.adapter.OnItemClickListener;
import com.anysoft.adapter.UserManageAddViewHolder;
import com.anysoft.adapter.UserManageViewHolder;

public class ItemRemoveRecyclerView extends RecyclerView {

    private static final String TAG = "ItemRemoveRecyclerView";
    private static final int DELETE_ITEM_WIDTH = 88;

    private Context mContext;

    //上一次的触摸点
    private int mLastX, mLastY;
    //当前触摸的item的位置
    private int mPosition;

    //item对应的布局
    private LinearLayout mItemLayout;
    //删除按钮
    private TextView mDelete;

    //最大滑动距离(即删除按钮的宽度)
    private int mMaxLength;
    //是否在垂直滑动列表
    private boolean isDragging;
    //item是在否跟随手指移动
    private boolean isItemMoving;

    //item是否开始自动滑动
    private boolean isStartScroll;
    //删除按钮状态   0：关闭 1：将要关闭 2：将要打开 3：打开
    private int mDeleteBtnState;

    //检测手指在滑动过程中的速度
    private VelocityTracker mVelocityTracker;
    private Scroller mScroller;
    private OnItemClickListener mListener;

    private long putDownTime;
    private long upTime;

    public ItemRemoveRecyclerView(Context context) {
        this(context, null);
    }

    public ItemRemoveRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ItemRemoveRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;

        mScroller = new Scroller(context, new LinearInterpolator());
        mVelocityTracker = VelocityTracker.obtain();
    }

    private boolean isItemTouch = false;
    private boolean isItemMove = false;
    private int wrongScrollX = 0;

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        mVelocityTracker.addMovement(e);

        int x = (int) e.getX();
        int y = (int) e.getY();

        View view = findChildViewUnder(x, y);

        ViewHolder mViewHolder;
        try {
            mViewHolder = getChildViewHolder(view);
        } catch (Exception exception) {
//            Logger.t(TAG).d(exception);
            return super.onTouchEvent(e);
        }

        if (mViewHolder != null) {
            if (mViewHolder instanceof UserManageViewHolder) {
                switch (e.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        isItemTouch = true;
                        putDownTime = System.currentTimeMillis();
                        if (isItemTouch && isItemMove) {
                            isItemTouch = false;
                            isItemMove = false;
                            mScroller.startScroll(mItemLayout.getScrollX(), 0, -wrongScrollX, 0, 200);
                            invalidate();
                            mDeleteBtnState = 0;
                            return false;
                        } else {
                            if (mDeleteBtnState == 0) {
                                if (view == null) {
                                    return false;
                                }

                                UserManageViewHolder viewHolder = (UserManageViewHolder) mViewHolder;

                                mItemLayout = viewHolder.layout;
                                mPosition = viewHolder.getAdapterPosition();

                                mDelete = (TextView) mItemLayout.findViewById(R.id.item_delete);
                                mMaxLength = mDelete.getWidth();
                                mDelete.setOnClickListener(new OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        mListener.onDeleteClick(mPosition);
                                        mItemLayout.scrollTo(0, 0);
                                        mDeleteBtnState = 0;
                                    }
                                });
                            } else if (mDeleteBtnState == 3) {
                                mScroller.startScroll(mItemLayout.getScrollX(), 0, -mMaxLength, 0, 200);
                                invalidate();
                                mDeleteBtnState = 0;
                                return false;
                            } else {
                                return false;
                            }
                        }

                        break;
                    case MotionEvent.ACTION_MOVE:
                        isItemMove = true;
                        int dx = mLastX - x;
                        int dy = mLastY - y;

                        int scrollX = mItemLayout.getScrollX();
                        wrongScrollX = scrollX + dx;
                        //System.out.println("scrollX---" + scrollX + ",x=" + x + ",dx=" + dx);
                        if (Math.abs(dx) > Math.abs(dy)) {//左边界检测
                            isItemMoving = true;
                            if (scrollX + dx <= 0) {
                                mItemLayout.scrollTo(0, 0);
                                return true;
                            } else if (scrollX + dx >= mMaxLength) {//右边界检测
                                mItemLayout.scrollTo(mMaxLength, 0);
                                return true;
                            }
                            mItemLayout.scrollBy(dx, 0);//item跟随手指滑动
                        }
                        break;
                    case MotionEvent.ACTION_UP:

                        upTime = System.currentTimeMillis();

                        if (!isItemMoving && !isDragging && mListener != null) {
                            if (!checkLongClick()) {
                                mListener.onItemClick(mItemLayout, mPosition);
                            } else {
                                //TODO longclick
                            }
                        }
                        isItemMoving = false;

                        mVelocityTracker.computeCurrentVelocity(1000);//计算手指滑动的速度
                        float xVelocity = mVelocityTracker.getXVelocity();//水平方向速度（向左为负）
                        float yVelocity = mVelocityTracker.getYVelocity();//垂直方向速度

                        int deltaX = 0;
                        int upScrollX = mItemLayout.getScrollX();

                        if (Math.abs(xVelocity) > DELETE_ITEM_WIDTH && Math.abs(xVelocity) > Math.abs(yVelocity)) {
                            if (xVelocity <= -DELETE_ITEM_WIDTH) {//左滑速度大于DELETE_ITEM_WIDTH，则删除按钮显示
                                deltaX = mMaxLength - upScrollX;
                                mDeleteBtnState = 2;
                            } else if (xVelocity > DELETE_ITEM_WIDTH) {//右滑速度大于DELETE_ITEM_WIDTH，则删除按钮隐藏
                                deltaX = -upScrollX;
                                mDeleteBtnState = 1;
                            }
                        } else {
                            if (upScrollX >= mMaxLength / 2) {//item的左滑动距离大于删除按钮宽度的一半，则则显示删除按钮
                                deltaX = mMaxLength - upScrollX;
                                mDeleteBtnState = 2;
                            } else if (upScrollX < mMaxLength / 2) {//否则隐藏
                                deltaX = -upScrollX;
                                mDeleteBtnState = 1;
                            }
                        }

                        //item自动滑动到指定位置
                        mScroller.startScroll(upScrollX, 0, deltaX, 0, 200);
                        isStartScroll = true;
                        invalidate();

                        mVelocityTracker.clear();

                        isItemTouch = false;
                        isItemMove = false;
                        break;
                }

                mLastX = x;
                mLastY = y;
                return super.onTouchEvent(e);
            } else {
                switch (e.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        putDownTime = System.currentTimeMillis();

                        if (isItemTouch && isItemMove) {
                            isItemTouch = false;
                            isItemMove = false;
                            //System.out.println("wrongScrollX---" + wrongScrollX);
                            mScroller.startScroll(mItemLayout.getScrollX(), 0, -wrongScrollX, 0, 200);
                            invalidate();
                            mDeleteBtnState = 0;
                            return false;
                        } else {
                            if (mDeleteBtnState == 0) {
                                if (view == null) {
                                    return false;
                                }

                                UserManageAddViewHolder viewHolder = (UserManageAddViewHolder) mViewHolder;

                                mItemLayout = viewHolder.layout;
                                mPosition = viewHolder.getAdapterPosition();
                            } else if (mDeleteBtnState == 3) {
                                mScroller.startScroll(mItemLayout.getScrollX(), 0, -mMaxLength, 0, 200);
                                invalidate();
                                mDeleteBtnState = 0;
                                return false;
                            } else {
                                return false;
                            }
                        }

                        break;
                    case MotionEvent.ACTION_UP:

                        upTime = System.currentTimeMillis();

                        if (!checkLongClick()) {
                            mListener.onItemClick(mItemLayout, mPosition);
                        } else {
                            //TODO longclick
                        }
                        break;
                }
                mLastX = x;
                mLastY = y;
                return super.onTouchEvent(e);
            }
        } else {
            return super.onTouchEvent(e);
        }


    }

    private boolean checkLongClick() {
        return upTime - putDownTime > 500;
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            mItemLayout.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        } else if (isStartScroll) {
            isStartScroll = false;
            if (mDeleteBtnState == 1) {
                mDeleteBtnState = 0;
            }

            if (mDeleteBtnState == 2) {
                mDeleteBtnState = 3;
            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        mVelocityTracker.recycle();
        super.onDetachedFromWindow();
    }

    @Override
    public void onScrollStateChanged(int state) {
        super.onScrollStateChanged(state);
        isDragging = state == SCROLL_STATE_DRAGGING;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }
}
